package ent

import (
	"elegantGo/chapter17/oo"
)

func (entArticle *Article) Mapper() oo.Article {
	if entArticle == nil {
		return nil
	}

	var articleOrders ArticleOrders = entArticle.Edges.Orders

	return oo.NewArticleBuilder().
		SetID(entArticle.ID).
		SetCategory(entArticle.Category).
		SetTitle(entArticle.Title).
		SetSummary(entArticle.Summary).
		SetReadCount(entArticle.ReadCount).
		SetContent(entArticle.Content).
		SetPrice(entArticle.Price).
		SetSaleCount(entArticle.SaleCount).
		SetUser(entArticle.Edges.User.Mapper()).
		SetOrders(articleOrders.Mapper()).
		Build()
}

func (entArticles Articles) Mapper() oo.Articles {
	size := len(entArticles)
	ooArticles := make(oo.Articles, size)
	for i := 0; i < size; i++ {
		ooArticles[i] = entArticles[i].Mapper()
	}
	return ooArticles
}
